unit frmTreeEdit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ButtonPanel, ExtCtrls, StdCtrls, Buttons;

type

  { TFormTreeEdit }

  TFormTreeEdit = class(TForm)
    btnPanel: TButtonPanel;
    lblEdit: TLabeledEdit;
    procedure CancelButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure HelpButtonClick(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
  private
    { private declarations }
    procedure GetName();
  public
    { public declarations }

  end; 

var
  FormTreeEdit: TFormTreeEdit;

implementation

uses datamodule, main;
{ TFormTreeEdit }

procedure TFormTreeEdit.OKButtonClick(Sender: TObject);
var
   FieldParent: string;
begin

  case FormMain.TreeMain.Tag of
    0: begin
      DM.SQL_Q.Close;
      DM.SQL_Q.SQL.Clear;
      DM.SQL_Q.SQL.Add('INSERT INTO BTree VALUES (NULL, '+IntToStr(Integer(FormMain.TreeMain.Selected.Data))+', 0, '''+lblEdit.Text+''');');
      DM.SQL_Q.ExecSQL;
      DM.SQL_T.Commit;
    end;
    1: begin
      DM.SQL_Q.Close;
      DM.SQL_Q.SQL.Clear;
      DM.SQL_Q.SQL.Add ('SELECT * FROM BTree WHERE IDTree='+IntToStr(Integer(FormMain.TreeMain.Selected.Data))+';');
      DM.SQL_Q.Open;
      FieldParent:=IntToStr(DM.SQL_Q.FieldByName('Parent').asInteger);
      DM.SQL_Q.Close;
      DM.SQL_Q.SQL.Clear;
      DM.SQL_Q.SQL.Add('INSERT INTO BTree VALUES (NULL,'+FieldParent+', 0, '''+lblEdit.Text+''');');
      DM.SQL_Q.ExecSQL;
      DM.SQL_T.Commit;
    end;
    2: begin
      DM.SQL_Q.Close;
      DM.SQL_Q.SQL.Clear;
      DM.SQL_Q.SQL.Add('UPDATE BTree SET Name='''+lblEdit.Text+''' WHERE IDTree='+IntToStr(Integer(FormMain.TreeMain.Selected.Data))+';');
      DM.SQL_Q.ExecSQL;
      DM.SQL_T.Commit;
    end;
  end;

  FormTreeEdit.Close;
end;


procedure TFormTreeEdit.FormCreate(Sender: TObject);
begin
  btnPanel.OKButton.OnClick:=@OKButtonClick;
  btnPanel.CancelButton.OnClick:=@CancelButtonClick;
  btnPanel.HelpButton.OnClick:=@HelpButtonClick;
  GetName;
end;

procedure TFormTreeEdit.FormShow(Sender: TObject);
begin
  GetName;
end;

procedure TFormTreeEdit.GetName();
begin
  DM.SQL_Q.Close;
  DM.SQL_Q.SQL.Clear;
  DM.SQL_Q.SQL.Add('SELECT * FROM BTree WHERE IDTree= '+IntToStr(Integer(FormMain.TreeMain.Selected.Data))+';');
  DM.SQL_Q.Open;
  if FormMain.TreeMain.Tag = 2 then begin
    FormTreeEdit.lblEdit.Text:=DM.SQL_Q.FieldByName('Name').AsString;
  end else begin
    FormTreeEdit.lblEdit.Text:='Новая запись';
  end;
end;

procedure TFormTreeEdit.HelpButtonClick(Sender: TObject);
begin
// Formmain.Status.Panels.Items[2].Text:='HELP';
end;

procedure TFormTreeEdit.CancelButtonClick(Sender: TObject);
begin
  FormTreeEdit.Close;
end;

initialization
  {$I frmtreeedit.lrs}

end.

